package com.disapand.strongarmy.controller

import com.disapand.strongarmy.service.*
import com.disapand.strongarmy.utils.*
import com.disapand.strongarmy.vo.*
import org.springframework.web.bind.annotation.*

/**
 *
 * @auth disapand
 * @date 2021/10/31 - 17:27 - 星期日
 *
 */
@RestController
@RequestMapping("/permissions", name = "权限管理")
class PermissionController(private val permissionService: PermissionService) {

    /**
     * 获取权限组
     */
    @get:GetMapping("/groups", name = "获取所有权限组")
    val permissionGroup: ResponseVO
        get() {
            return ResponseVO.ofSuccess(permissionService.getPermissionAllGroup())
        }

    /**
     * 获取指定权限组的所有权限
     *
     * @param group
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/permission-by-group", name = "获取指定权限组的所有权限")
    fun getPermissionByGroup(
        @RequestParam("group") group: String,
        @RequestParam("current", required = false, defaultValue = "1") current: Int,
        @RequestParam("page_size", required = false, defaultValue = "20") pageSize: Int,
    ): ResponseVO {
        return ResponseVO.ofSuccess(permissionService.getPermissionByGroup(group, current, pageSize))
    }

    /**
     * 获取所有权限
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/list", name = "分页获取权限列表")
    fun permissions(
        @RequestParam("current", required = false, defaultValue = "1") current: Int,
        @RequestParam("page_size", required = false, defaultValue = "20") pageSize: Int,
    ): ResponseVO {
        return ResponseVO.ofSuccess(permissionService.get(current, pageSize))
    }

    /**
     * 以树形返回所有权限
     */
    @get:GetMapping("/tree", name = "树形获取所有权限")
    val permissionTree: ResponseVO
        get() {
            val result = SaUtils.generatePermissionTree(permissionService.get())
            return ResponseVO.ofSuccess(result)
        }
}